package com.solidtrust.admin.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.solidtrust.app.FileSystemUtil;
import com.solidtrust.entity.CarImage;
import com.solidtrust.util.ImageUtil;


@Service
public class ImageExtractor {
	Logger log4j = Logger.getLogger(ImageExtractor.class);
	
	
	public CarImage scrape(MultipartFile file, CarImage imageInfo, Integer order)throws IOException {
		log4j.info("Start to persist " + file.getName());
		File f = getImageFile(imageInfo.getSubBrandId(), order);
		File thumb = getThumbFile(imageInfo.getSubBrandId(), order);
		file.transferTo(f);
	
		log4j.info("Finish saving file " + file.getName());
		
		log4j.info("Resize image file");
		BufferedImage bi = ImageIO.read(f);
		if(bi != null){
			ImageUtil.resize(bi, f, ImageUtil.HEIGHT);
			ImageUtil.resize(bi, thumb, ImageUtil.THUMBNAL_HEIGHT);
			imageInfo.setStatus(0);
	        log4j.info("It is image file. Will save it to DB");
		}else{
			log4j.info("It is not image file. Will remove it");
			f.delete();
		}
		return imageInfo;
	}
	
	public CarImage scrape(String url, CarImage imageInfo, Integer order) throws IOException {
		log4j.info("Start to persist " + url);
		File f = getImageFile(imageInfo.getSubBrandId(), order);
		File thumb = getThumbFile(imageInfo.getSubBrandId(), order);
		FileUtils.copyURLToFile(new URL(url), f);
		log4j.info("Finish saving file " + url);
		
		log4j.info("Resize image file");
		BufferedImage bi = ImageIO.read(f);
		if(bi != null){
			ImageUtil.resize(bi, f, ImageUtil.HEIGHT);
			ImageUtil.resize(bi, thumb, ImageUtil.THUMBNAL_HEIGHT);
			imageInfo.setStatus(0);
			log4j.info("It is image file. Will save it to DB");
		}else{
			log4j.info("It is not image file. Will remove it");
			f.delete();
		}
	
		return imageInfo;
	}
	
	public static File getImageFile(String subBrandId, int index){
		File f = new File(FileSystemUtil.getCarImage(), getImageFilename(subBrandId, index));
		return f;
	}
	public static File getThumbFile(String subBrandId, int index){
		File f = new File(FileSystemUtil.getCarThumb(), getImageFilename(subBrandId, index));
		return f;
	}
	
	public static String getImageFilename(String subBrandId, int index){
		return subBrandId + "_" + index + ".jpg";
	}
	
	
	public static void main(String[] args) throws Exception{
		FileSystemUtil.setHome(new File("D:\\projects\\car"));
		int i = 0;
		for (File f : FileSystemUtil.getCarImage().listFiles()) {
			i ++;
			if(i < 815){
				continue;
			}
			BufferedImage bi = ImageIO.read(f);
			File thumb = new File(FileSystemUtil.getCarThumb(), f.getName());
			ImageUtil.resize(bi, thumb, ImageUtil.THUMBNAL_HEIGHT);
		}
	}
}
